package com.zrrd._01_jdbc;

import org.junit.Test;

import java.sql.*;


/**
 * @author WanJl
 * @version 1.0
 * @title Login
 * @description 实现登录功能
 * @create 2024/1/11 16:39
 */
public class UserDao {
    /**
     * 登录的方法
     * @param un
     * @param pwd
     * @return
     */
    public static boolean login(String un,String pwd) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.创建和数据库的连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jms2301_db01", "root", "123456");
        //3.创建Statement对象
        Statement stat = conn.createStatement();
        //4.编写SQL语句
        String sql="SELECT * FROM jms2301_db01.user WHERE username='"+un+"' AND password= '"+pwd+"'";
        // SELECT * FROM jms2301_db01.user WHERE username='zhangsan' AND password= '123465'
        //5.执行SQL语句，获取返回结果
        ResultSet rs = stat.executeQuery(sql);
        boolean flag=false;
        //6.处理结果
        if (rs.next()){
            flag=true;
        }
        //7.释放资源
        rs.close();
        stat.close();
        conn.close();

        return flag;
    }

    public static boolean newLogin(String un,String pwd) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.创建和数据库的连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jms2301_db01", "root", "123456");
        //3.编写SQL语句
        String sql="SELECT * FROM jms2301_db01.user WHERE username= ? AND password= ?";
        //4.创建PreparedStatement对象，并且针对sql语句进行预编译
        PreparedStatement pstat = conn.prepareStatement(sql);
        //5.为占位符(?)设置值
        pstat.setString(2,pwd);
        pstat.setString(1,un);
        //6.直接执行已经预编译好的SQL语句，并且获取返回结果
        ResultSet rs = pstat.executeQuery();
        boolean flag=false;
        //7.处理结果
        if (rs.next()){
            flag=true;
        }
        //8.释放资源
        rs.close();
        pstat.close();
        conn.close();

        return flag;
    }



    /**
     * 注册的方法
     * @param un
     * @param pwd
     * @return
     */
    public boolean register(String un,String pwd) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.创建和数据库的连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jms2301_db01", "root", "123456");
        //3.创建Statement对象
        Statement stat = conn.createStatement();
        //4.编写SQL语句
        String sql="INSERT INTO jms2301_db01.user (username, password) VALUES ('"+un+"','"+pwd+"')";
        //5.执行SQL语句，获取返回结果
        int count = stat.executeUpdate(sql);
        boolean flag=false;
        //6.处理结果
        if (count>0){
            flag=true;
        }
        //7.释放资源
        stat.close();
        conn.close();

        return flag;
    }

    @Test
    public void testLogin() throws SQLException, ClassNotFoundException {
        // SELECT * FROM user WHERE username='zhangsan' AND password='' or '1' = '1';
        boolean result = login("zhangsan", "' or '1' = '1");
        if (result){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
    }

    @Test
    public void testRegister() throws SQLException, ClassNotFoundException {
        boolean result = register("lisi", "123456");
        if (result){
            System.out.println("注册成功");
        }else {
            System.out.println("注册失败");
        }

    }

    @Test
    public void testNewLogin() throws SQLException, ClassNotFoundException {
        // SELECT * FROM user WHERE username='zhangsan' AND password='' or '1' = '1';
        boolean result = newLogin("zhangsan", "' or '1' = '1");
        if (result){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }
    }
}
